library(ggplot2)
Need help? Try Stackoverflow: https://stackoverflow.com/tags/ggplot2
library(plotly)
Attaching package: ‘plotly’
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout
df = read.csv('Expectancy.csv', header = TRUE, sep = ",")
head(df)
tail(df)
str(df)
'data.frame': 1704 obs. of 7 variables:
$ X : int 1 2 3 4 5 6 7 8 9 10 ...
$ country : chr "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
$ continent: chr "Asia" "Asia" "Asia" "Asia" ...
$ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
$ lifeExp : num 28.8 30.3 32 34 36.1 ...
$ pop : int 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
$ gdpPercap: num 779 821 853 836 740 ...
attr(x = df, which = "names")
[1] "X" "country" "continent" "year" "lifeExp" "pop" "gdpPercap"
summary(df)
X country continent year lifeExp pop gdpPercap
Min. : 1.0 Length:1704 Length:1704 Min. :1952 Min. :23.60 Min. :6.001e+04 Min. : 241.2
1st Qu.: 426.8 Class :character Class :character 1st Qu.:1966 1st Qu.:48.20 1st Qu.:2.794e+06 1st Qu.: 1202.1
Median : 852.5 Mode :character Mode :character Median :1980 Median :60.71 Median :7.024e+06 Median : 3531.8
Mean : 852.5 Mean :1980 Mean :59.47 Mean :2.960e+07 Mean : 7215.3
3rd Qu.:1278.2 3rd Qu.:1993 3rd Qu.:70.85 3rd Qu.:1.959e+07 3rd Qu.: 9325.5
Max. :1704.0 Max. :2007 Max. :82.60 Max. :1.319e+09 Max. :113523.1
lapply(df,function(x) {
length(which(is.na(x)))
})
$X
[1] 0
$country
[1] 0
$continent
[1] 0
$year
[1] 0
$lifeExp
[1] 0
$pop
[1] 0
$gdpPercap
[1] 0
df_india = subset(df,country =="India")
df_india
ggplot(df_india, aes(x=year, y=lifeExp)) +
geom_point(size = 3, color = "#0099f9") +
labs(
title = "Life Expectancy vs Year",
subtitle = "LifeExp - Expected number of years remaining for an individual at any given year ",
caption = "Source: Expectancy dataset"
) +
theme(
plot.title = element_text(color = "#0099f9", size = 20, face = "bold", hjust = 0.5),
plot.subtitle = element_text(size = 13, face = "bold", hjust = 0.5),
plot.caption = element_text(face = "italic", hjust = 0)
)
fig = plot_ly(data = df_india, x = ~year, y = ~lifeExp,
type = 'scatter', mode = 'lines',
marker = list(size = 10,
color = 'rgba(255, 182, 193, .9)',
line = list(color = 'rgba(152, 0, 0, .8)',width = 2)))
fig <- fig %>% layout(title = 'Life Expectancy vs Year',
yaxis = list(zeroline = FALSE),
xaxis = list(zeroline = FALSE)
)
fig
A marker object has been specified, but markers is not in the mode
Adding markers to the mode...
A marker object has been specified, but markers is not in the mode
Adding markers to the mode...
## Take out the five countries
df_5countries<-df[df$country %in% c('China', 'India', 'France', 'Poland', 'Serbia'), ]
head(df_5countries)
fig = plot_ly(data = df_5countries, x = ~year, y = ~lifeExp, color=~country,
marker = list(size = 10))
fig <- fig %>% layout(title = 'Life Expectancy vs Year',
yaxis = list(zeroline = FALSE),
xaxis = list(zeroline = FALSE)
)
fig
No trace type specified:
Based on info supplied, a 'scatter' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#scatter
No scatter mode specifed:
Setting the mode to markers
Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
No trace type specified:
Based on info supplied, a 'scatter' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#scatter
No scatter mode specifed:
Setting the mode to markers
Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
ggplot(df_5countries, aes(x=year, y=lifeExp, color=country)) +
geom_line(size = 1) +
labs(
title = "Life Expectancy by year for five countries.",
caption = "Source: Expectancy dataset"
) +
theme(
plot.title = element_text(color = "#0099f9", size = 20, face = "bold", hjust = 0.5),
plot.subtitle = element_text(size = 13, face = "bold", hjust = 0.5),
plot.caption = element_text(face = "italic", hjust = 0)
)
#3. Plot the life expectancy of all the countries in Europe for any particular year.
## Take out Europian Continent
df_europe2007 = subset(df,continent =="Europe"&year==2007)
head(df_europe2007)
fig = plot_ly(data = df_europe2007, x = ~country, y = ~lifeExp,
marker = list(size = 10))
fig <- fig %>% layout(title = 'Life Expectancy in Europen Continent in 2007 by year',
yaxis = list(zeroline = FALSE),
xaxis = list(zeroline = FALSE)
)
fig
No trace type specified:
Based on info supplied, a 'bar' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#bar
No trace type specified:
Based on info supplied, a 'bar' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#bar